package org.dromara.app.controller;

import cn.dev33.satoken.annotation.SaIgnore;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import org.dromara.bean.constant.CommonCode;
import org.dromara.bean.domain.HisRegion;
import org.dromara.bean.domain.model.HisRegionModel;
import org.dromara.bean.service.IBaseRegionService;
import org.dromara.bean.service.IHisMemberService;
import org.dromara.bean.util.BaiduMapUtil;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.utils.StringUtils;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
 * 地区管理
 */
@RestController
@RequestMapping("/app/region")
@Slf4j
@SaIgnore
@RequiredArgsConstructor
public class AHisRegionController {

    private  final IHisMemberService memberService;

    private  final IBaseRegionService baseRegionService;

    /**
     * 患者市级列表
     */
    @GetMapping(value = "/city")
    public R<?> queryList(@RequestParam(name = "cityName", defaultValue = "") String cityName) {
        List<HisRegionModel> region = memberService.listByAll(cityName);
        List<HisRegionModel> citys = new ArrayList<>();
        for (HisRegionModel city : region) {
            if ("city".equals(city.getFlag())) {
                citys.add(city);
            }
        }
        return R.ok(citys);
    }

    /**
     * 热门城市列表
     */
    @GetMapping(value = "/hotCity")
    public R<?> hotList() {
        List<HisRegionModel> region = memberService.listAll();
        List<HisRegionModel> citys = new ArrayList<>();
        for (HisRegionModel city : region) {
            if ("city".equals(city.getFlag()) && "1".equals(city.getIsHot())) {
                citys.add(city);
            }
        }
        return R.ok(citys);
    }

    /**
     * 根据上级获取下级地区
     */
    @GetMapping(value = "/getAreaByPid")
    public R<?> getAreaByPid(@RequestParam(name = "pId", defaultValue = "") String pId) {
        List<HisRegionModel> region = memberService.listAll();
        List<HisRegionModel> result = new ArrayList<>();
        for (HisRegionModel hisRegionModel : region) {
            if (StringUtils.isNotEmpty(pId)) {
                int parentId = Integer.parseInt(pId);
                int dbPid = hisRegionModel.getParentId();
                if (parentId == dbPid) {
                    result.add(hisRegionModel);
                }
            } else {
                if ("province".equals(hisRegionModel.getFlag())) {
                    result.add(hisRegionModel);
                }
            }

        }
        return R.ok(result);
    }

    /**
     * 根据地区编码获取地区
     */
    @GetMapping(value = "/getByCode")
    public R<?> getRegion(@RequestParam(name = "regionCode")  String regionCode) {
        String code = regionCode.substring(3);
        HisRegionModel hisRegionModel = memberService.getByRegionCode(code);
        return R.ok(hisRegionModel);
    }

    /**
     * 根据经纬度返回市级区域
     */
    @GetMapping(value = "/getRegion")
    public R<?> getRegion(@RequestParam(name = "lat") String lat,
                               @RequestParam(name = "lng") String lng) {
        String regionCode = BaiduMapUtil.getRegionCode(lat, lng);
        if (StringUtils.isEmpty(regionCode)) {
            return R.errorCode(CommonCode.ENTITY_NULL_ERROR);
        }
        HisRegionModel hisRegionModel = memberService.getByRegionCode(regionCode);
        if (hisRegionModel != null) {
            QueryWrapper queryWrapper = new QueryWrapper();
            queryWrapper.eq("id", hisRegionModel.getParentId());
            HisRegion hisRegion = baseRegionService.getOne(queryWrapper);
            return R.ok(hisRegion);
        }
        return R.error(500,"获取地区失败");
    }

    /**
     * 根据PID分页列表查询
     */
    @GetMapping(value = "/listByPid")
    public R<?> listByPid(@RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                               @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
                               @RequestParam(name = "pid", defaultValue = "0") Integer pid) {
        return R.ok(baseRegionService.page(new Page<>(pageNo, pageSize),
                new LambdaQueryWrapper<HisRegion>().eq(HisRegion::getParentId, pid)
                        .orderByAsc(HisRegion::getRegionCode)));
    }
}
